<?
//ob
ob_start();

//configuration
include "../conf.php";

//includes
include "../php/functions.php";

//connect2db
$_DB = new PDO("mysql:host={$_MYSQL['host']};dbname={$_MYSQL['db_name']}", $_MYSQL['db_user'], $_MYSQL['password']);

if($_POST['do']=='wallstream') {
	$query=$_POST['query'];
	$description=$_POST['description'];
	$picture=$_POST['picture'];
	$query=$_POST['query'];
	$name=$_POST['lname'];
	$link=$_POST['link'];
	$message=$_POST['message'];
	$delay=intval($_POST['delay']);
	$intervall=intval($_POST['intervall']);

	$users=$_DB->query($query)->fetchAll();
	$task=array('message'=>$message,'description'=>$description,'link'=>$link,'picture'=>$picture,'name'=>$name);
	$count=0;
	foreach($users as $user) {
		$utask=array_merge($task, array('email'=>$user['email']));
		$task_json=json_encode($utask,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);
		//~ echo $task_json,"\n";
		$_DB->prepare("INSERT INTO `cronjobs`(`time`,`type`,`data`) VALUES(:time , :type , :data)")->execute(array(
			"time"=>time()+$delay+($intervall*$count++),
			"type"=>"wallstream",
			"data"=>$task_json
		));
	}
	echo "Everything is OK<br>";
}else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h1>Add WallJobs to database</h1>
<form method="post">
	<input type="hidden" name="do" value="wallstream">
	Filter:<br>
	<textarea name="query" style="width:500px">SELECT * FROM `user` WHERE `rights` LIKE "%publish_stream%" AND `authtoken`!="" AND 0</textarea><br>
	Message:<br>
	<textarea name="message" style="width:500px;height:200px">Hey!</textarea><br>
	Link:<br>
	<input name="link" value="http://goo.gl/PxWXA"><br>
	Name (Link):<br>
	<input name="lname" value="Listen to Ballads n Bullets by In Legend. Now!"><br>
	Description (Link):<br>
	<input name="description" value="Ballads N Bullets will be available May 20th 2011"><br>
	Picture:<br>
	<input name="picture" value="http://fbapp.rene-pickhardt.de/images/IL_150.jpg"><br>
	StartDelay<br>
	<input name="startdelay" value=60><br>
	Intervall between jobs:<br>
	<input name="intervall" value=0><br>
	<input type=submit value="start!">
</form>
</body>
<?
}
?>